/*
 * Copyright (c) 2007 jNetX.
 * http://www.jnetx.com
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * jNetX. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license
 * agreement you entered into with jNetX.
 *
 * $Id$
 */
package com.jnetx.javax.sip.message;

import com.jnetx.javax.sip.address.AddressFactoryImpl;
import com.jnetx.javax.sip.address.SipURIImpl;
import junit.framework.TestCase;

import javax.sip.address.Address;
import javax.sip.header.*;
import java.text.ParseException;

/**
 * @author <a href="mailto:dparhonin@jnetx.ru">Dmitry Parhonin</a>
 * @version $Revision$
 */
public class SIPRequestTest extends TestCase {
    final String data = "INVITE sip:bob@biloxi.com SIP/2.0\r\n" +
            "Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds\r\n" +
            "Max-Forwards: 70\r\n" +
            "To: Bob <sip:bob@biloxi.com>\r\n" +
            "From: Alice <sip:alice@atlanta.com>;tag=1928301774\r\n" +
            "Call-ID: a84b4c76e66710@pc33.atlanta.com\r\n" +
            "CSeq: 314159 INVITE\r\n" +
            "Contact: <sip:alice@pc33.atlanta.com>\r\n" +
            "Content-Type: application/sdp\r\n" +
            "Content-Length: 142\r\n\r\n";

    public SIPRequestTest(String name) {
        super(name);
    }

    public void testLazyCreation() {
        SIPRequest r = SIPRequest.createLazily(data.getBytes());
        assertEquals("The data changed!", data, r.toString());
        assertEquals("The method is wrong!", "INVITE", r.getMethod());
        try {
            assertEquals("The request URI is wrong!", AddressFactoryImpl.createURILazily("sip:bob@biloxi.com"), r.getRequestURI());
        } catch (ParseException e) {
            fail("Cannot create URI for checking!");
        }
        assertEquals("The SIP version is wrong!", "SIP/2.0", r.getSIPVersion());
        //Checking Via
        ViaHeader via = (ViaHeader) r.getHeader(ViaHeader.NAME);
        assertNotNull("The Via header is null!", via);
        assertEquals("The protocol is wrong!", "SIP/2.0", via.getProtocol());
        assertEquals("The transport is wrong!", "UDP", via.getTransport());
        assertEquals("The host is wrong!", "pc33.atlanta.com", via.getHost());
        assertEquals("The branch is wrong!", "z9hG4bK776asdhds", via.getBranch());
        //Checking Max-Forwards
        MaxForwardsHeader maxForwards = (MaxForwardsHeader) r.getHeader(MaxForwardsHeader.NAME);
        assertNotNull("The Max-Forwards header is null!", maxForwards);
        assertEquals("The value of Max-Forwards header is wrong!", 70, maxForwards.getMaxForwards());
        //Checking To
        ToHeader to = (ToHeader) r.getHeader(ToHeader.NAME);
        assertNotNull("The to header is null!", to);
        Address address = AddressFactoryImpl.createAddress();
        try {
            address.setDisplayName("Bob");
        } catch (ParseException e) {
            fail("Cannot set display name!");
        }
        address.setURI(new SipURIImpl("bob", "biloxi.com"));
        assertEquals("The address is wrong!", address, to.getAddress());
        //Checking From
        FromHeader from = (FromHeader) r.getHeader(FromHeader.NAME);
        assertNotNull("The from header is null!", from);
        address = AddressFactoryImpl.createAddress();
        try {
            address.setDisplayName("Alice");
        } catch (ParseException e) {
            fail("Cannot set display name!");
        }
        SipURIImpl uri = new SipURIImpl("alice", "atlanta.com");
        address.setURI(uri);
        try {
            from.setParameter("tag", "1928301774");
        } catch (ParseException e) {
            fail("Cannot set tag parameter!");
        }
        assertEquals("The address is wrong!", address, from.getAddress());
        //Checking Call-ID
        CallIdHeader callId = (CallIdHeader) r.getHeader(CallIdHeader.NAME);
        assertNotNull("The CallID header is null!", callId);
        assertEquals("The CallId is wrong!", "a84b4c76e66710@pc33.atlanta.com", callId.getCallId());
        //Checking CSeq
        CSeqHeader cSeq = (CSeqHeader) r.getHeader(CSeqHeader.NAME);
        assertNotNull("CSeq header is null!", cSeq);
        assertEquals("The CSeq method is wrong!", SIPRequest.INVITE, cSeq.getMethod());
        assertEquals("The CSeq sequence number is wrong!", 314159L, cSeq.getSeqNumber());
        //Checking Contact
        ContactHeader contact = (ContactHeader) r.getHeader(ContactHeader.NAME);
        assertNotNull("The contact header is null!", contact);
        assertNull("The contact name mustn't be specified!", contact.getAddress().getDisplayName());
        assertEquals("The contact address is wrong!", "sip:alice@pc33.atlanta.com", contact.getAddress().getURI().toString());
        //Checking ContentType
        ContentTypeHeader contentType = (ContentTypeHeader) r.getHeader(ContentTypeHeader.NAME);
        assertNotNull("The content type header is null!", r);
        assertEquals("The content type is wrong", "application", contentType.getContentType());
        assertEquals("The content subtype is wrong", "sdp", contentType.getContentSubType());
        //Checking content length
        ContentLengthHeader contentLength = (ContentLengthHeader) r.getHeader(ContentLengthHeader.NAME);
        assertNotNull("The content length header is null!", contentLength);
        assertEquals("The content length is wrong!", 142, contentLength.getContentLength());
    }
}
/*
 * $Log$
 */